<template>
  <div>
    <van-nav-bar title="黑马头条" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        type="tel"
        name="手机号"
        label="手机号"
        placeholder="请输入手机号"
        required
        :rules="[
          {
            required: true,
            message: '请填写正确的手机号',
            trigger: 'onChange',
            pattern: /^1[3-4]\d{9}$/,
          },
        ]"
      />
      <van-field
        v-model="user.code"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        required
        :rules="[
          {
            required: true,
            message: '请填写密码',
            trigger: 'onChange',
            pattern: /^\d{6}$/,
          },
        ]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
        :loading="isloading"
        :disabled="isloading"
        loading-type="spinner"
        loading-text="程序员狂掉头发中........"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
// 通知组件
import { Notify } from 'vant'
export default {
  name: 'Login',
  data () {
    return {
      name: 'Login',
      user: {
        mobile: '13888888888',
        code: '246810'
      },
      isloading: false
    }
  },
  components: {
    // 注册通知组件
    [Notify.Component.name]: Notify.Component
  },
  methods: {
    ...mapActions(['LoginAction']),
    async onSubmit (values) {
      this.isloading = true
      // 调用接口
      try {
        // 1.引入接口
        // 2.在方法中调用接口
        // 3.调用接口如果需要传参数，需要将传入的参数整理传入
        await this.LoginAction(this.user)
        Notify({ type: 'success', message: '登陆成功' })
        this.isloading = false
        this.$router.replace({
          path: '/layout'
        })
      } catch (error) {
        this.isloading = false
        Notify({ type: 'danger', message: '登陆失败，请检查您的账号或密码书写是否正确' })
      }
    }
  }
}
</script>

<style lang="less" scoped>
// .van-nav-bar {
//   background: #007bff;
// }

// /* /deep/ 就是把data-v-hash值选择器写到类名的前面 */
// /deep/ .van-nav-bar__title {
//   color: white;
// }
</style>
